﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DB = HTComics.Data;

namespace HTComics.Web.Admin
{
    public partial class Discount : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Session["IsAdmin"] == null || !Convert.ToBoolean(Session["IsAdmin"]))
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadDiscountDetails();
        }

        private void LoadDiscountDetails()
        {
            DataTable dtDiscountDet = DB.Misc.GetDiscountDetails();

            txtFactor.Value = dtDiscountDet.Rows[0]["DiscountFactor"].ToString();
            chkEnabled.Checked = (dtDiscountDet.Rows[0]["Enabled"].ToString() == "1") ? true : false;
            lblLastUpdated.Text = Convert.ToDateTime(dtDiscountDet.Rows[0]["TimeStamp"].ToString()).ToString("MMM dd yyyy");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            DB.Admin.UpdateDiscountDetails(txtFactor.Value, chkEnabled.Checked ? "1" : "0");

            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>success();</script>");
        }
    }
}